<?php

namespace app\admin\model;

use think\Model;

class Role extends Model
{
  public static function list()
  {
    $list = self::select();
    foreach ($list as $k => $v) {
      $authDesc = [];
      if ($v->auth != 'all') {
        $authArr = explode(',', $v->auth);
        foreach ($authArr as $m => $n) {
          $desc = Auth::find($n);
          if ($desc) {
            array_push($authDesc, $desc->description);
          }
        }
        $list[$k]['desc'] = $authDesc;
      } else unset($list[$k]);
    }
    $data = ['list' => $list, 'count' => count($list)];
    return $data;
  }
  /**
   *  表单得到的角色id，分割角色id 1,2
   * 得到角色ID字符串 
   */
  public static function ArrtoStr($arr)
  {
    $idArr = [];
    if (is_array($arr)) {
      foreach ($arr as $k => $v) {
        $hasRole = self::findOrEmpty($k)->isEmpty();
        if (!$hasRole) {
          array_push($idArr, $k);
        }
      }
      return  implode(',', $idArr);
    }
  }
  // 参数2,3,4 角色ID 返回为角色名称
  public static function getRoleName($roleIdStr)
  {
    $desc = '';
    if ($roleIdStr == 'all') {
      $desc .= 'all,';
    } else {
      if (!empty($roleIdStr)) {
        $roleArr = explode(',', $roleIdStr);
        foreach ($roleArr as $m => $n) {
          $role = Role::find($n);
          if ($role) {
            $desc .=  $role['name'] . ',';
          }
        }
      }else{
        return '';
      }
    }
    return $desc;
  }
}
